package com.example.xjtuse_pc.receiver;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;

import com.example.xjtuse_pc.mobilesafe.R;
import com.example.xjtuse_pc.services.GPSService;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.Objects;

/**
 * Created by XJTUSE-PC on 2016/3/27.
 */
public class SmsReceiver extends BroadcastReceiver {
    private SharedPreferences sp;
    private DevicePolicyManager dpm;
    @Override
    public void onReceive(Context context, Intent intent) {

        sp=context.getSharedPreferences("config",Context.MODE_PRIVATE);
        dpm= (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
       Object[] obj= (Object[]) intent.getExtras().get("pdus");
        for(Object b:obj){
            String number=sp.getString("safephone","");
            SmsMessage sms =SmsMessage.createFromPdu((byte[])b);
            String sender=sms.getOriginatingAddress();
            String body=sms.getMessageBody();
            Log.i("join", "====sender==" + sender);
            if(sender.contains(number)){
                if("#*location*#".equals(body)){
                    //GPS
                    Intent i=new Intent(context, GPSService.class);
                    context.startService(i);
                    String location=sp.getString("location",null);
                    if(TextUtils.isEmpty(location)){
                        SmsManager.getDefault().sendTextMessage(sender,null,"geting loaction.....",null,null);
                    }else{
                        SmsManager.getDefault().sendTextMessage(sender,null,location,null,null);
                    }
                    Log.i("join",location);
                    abortBroadcast();
                }else if("#*alarm*#".equals(body)){
                    //警报
                    Log.i("join", "#*alarm*#");
                    MediaPlayer player=MediaPlayer.create(context, R.raw.alarm);
                    //循环播放
                    player.setLooping(false);
                    player.setVolume(1.0f,1.0f);
                    player.start();
                    abortBroadcast();
                }else if("#*wipedata*#".equals(body)){
                    //清除数据
                    //dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//清除sdcard所有数据
                    //dpm.wipeData(0);//恢复出厂设置
                    Log.i("join","#*wipedata*#");
                    abortBroadcast();
                }else if("#*lockscreen*#".equals(body)){
                    //锁屏
                    dpm.lockNow();
                    dpm.resetPassword("", 0);//设置开启密码
                    Log.i("join","#*lockscreen*#");
                    abortBroadcast();
                }
            }
            }

    }




}
